<?php

add_action( 'bpdev_widgets_extra_admin_save', 'bpdev_widgets_profile_friends_random_admin_save' );
add_action( 'bpdev_widgets_extra_admin_screen', 'bpdev_widgets_profile_friends_random_admin_screen' );

if ( !get_option( 'bpdev-widgets-profile_friends_random-status' ) ) add_option( 'bpdev-widgets-profile_friends_random-status', 'off' );

if ( 'on' == get_option( 'bpdev-widgets-status' ) )
	if ( 'on' == get_option( 'bpdev-widgets-profile_friends_random-status' ) )
		add_action( 'bpdev_widget_profile' , 'bpdev_widgets_profile_friends_random' , 1 );

function bpdev_widgets_profile_friends_random_admin_screen() {

?>

		<tr>

			<th scope="row">Profile Friends</th>
			<td>

				<select id="bpdev-widgets-profile_friends_random-status" name="bpdev-widgets-profile_friends_random-status">

					<option value="off"  <?php if ( 'off' == get_option( 'bpdev-widgets-profile_friends_random-status' ) ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( 'on' == get_option( 'bpdev-widgets-profile_friends_random-status' ) ) echo 'selected=""'; ?>>On</option>

				</select>

			</td>

		</tr>

<?php

}

function bpdev_widgets_profile_friends_random_admin_save() {

	do_action( 'bpdev_admin_save_component', 'bpdev-widgets-profile_friends_random-status' );

}

function bpdev_widgets_profile_friends_random() {

	global $bp;
	
	$friend_ids = BP_Friends_Friendship::get_random_friends( $bp->displayed_user->id , 28 );

	?>

	<div class="info-group">

		<h4><?php bp_word_or_name( __( "Friends", 'buddypress' ), __( "Friends", 'buddypress' ) ) ?>  (<?php echo BP_Friends_Friendship::total_friend_count( $bp->displayed_user->id ) ?>)  <a href="<?php echo $bp->displayed_user->domain . $bp->friends->slug ?>"><?php _e('All', 'buddypress') ?> &raquo;</a></h4>
		
		<?php if ( $friend_ids ) { ?>

			<ul class="bpdev-widgets profile-widget bpdev-widget-avatar-list" id="bpdev-profile-friends">

			<?php for ( $i = 0; $i < count( $friend_ids ); $i++ ) { ?>
				<li>
					<a href="<?php echo bp_core_get_userurl( $friend_ids[$i] ) ?>"><?php echo bp_core_get_avatar( $friend_ids[$i], 1, 30, 30 ) ?></a>
				</li>
			<?php } ?>

			</ul>

		<?php } else { ?>

			<div id="message" class="info">
				<p><?php bp_word_or_name( __( "You haven't added any friend connections yet.", 'buddypress' ), __( "%s hasn't created any friend connections yet.", 'buddypress' ) ) ?></p>
			</div>

		<?php } ?>

		<div class="clear"></div>
	</div>

	<?php

}

?>